package com.neteast.androidclient.newscenter;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import com.neteast.androidclient.newscenter.activity.LoginActivity;
import com.neteast.androidclient.newscenter.activity.QQWebViewActivity;
import com.neteast.androidclient.newscenter.db.InfoDao;
import com.neteast.androidclient.newscenter.domain.Information;
import com.neteast.androidclient.newscenter.provider.LoginInfoProvider;
import com.neteast.androidclient.newscenter.util.InfoUtil;
import com.neteast.androidclient.newscenter.util.JsonUtil;
import com.tencent.weibo.api.User_API;
import com.tencent.weibo.beans.OAuth;
import com.tencent.weibo.utils.Base64Encoder;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.test.AndroidTestCase;
import android.util.Log;
import android.widget.TextView;

public class Test extends AndroidTestCase {
	
	public void testContentProviderQuery() throws Throwable{
		Context context = getContext();
		Cursor cursor = context.getContentResolver().query(LoginInfoProvider.LoginInfo.CONTENT_URI, null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				int userid = cursor.getInt(cursor.getColumnIndex(LoginInfoProvider.LoginInfo.USERID));
				String token = cursor.getString(cursor.getColumnIndex(LoginInfoProvider.LoginInfo.TOKEN));
				Log.i("test", "userid="+userid);
				Log.i("test", "token="+token);
			} while (cursor.moveToNext());
		}
	}
	public void testContentProviderInsert() throws Throwable{
		Context context = getContext();
		ContentValues values=new ContentValues();
		values.put(LoginInfoProvider.LoginInfo.USERID, 0);
		values.put(LoginInfoProvider.LoginInfo.TOKEN, "");
		context.getContentResolver().insert(LoginInfoProvider.LoginInfo.CONTENT_URI, values);
	}
	
	public void testGetUsetInfo() throws Throwable{
		Bundle userCenterInfo = InfoUtil.getUserCenterInfo(getContext());
		Log.i("test", userCenterInfo.toString());
	}
	public void testEditUsetInfo() throws Throwable{
		Bundle userCenterInfo = InfoUtil.getUserCenterInfo(getContext());
		userCenterInfo.putString("QQ", "654037451");
		JSONObject json = JsonUtil.parseUserCenterInfo2Json(getContext(), userCenterInfo);
		InfoUtil.editUserCenterInfo(getContext(), json);
	}
	
	public void testEmpty() throws Throwable{
		TextView textView=new TextView(getContext());
		Bundle bundle=new Bundle();
		textView.setText(bundle.getString("aaa"));
	}
	
	public void testInfoDao() throws Throwable{
		InfoDao dao=new InfoDao(getContext());
		dao.open();
		ArrayList<Information> sysInfos = dao.getSysInfos(51);
		if (sysInfos!=null) {
			Log.i("test", sysInfos.toString());
		}
		dao.close();
	}
	public void testQQ() throws Throwable{
		if (QQWebViewActivity.isValidate(getContext())) {
			OAuth oAuth = QQWebViewActivity.getOAuth(getContext());
			User_API api=new User_API();
			String info = api.info(oAuth, "json");
			Log.i("test", "info="+info);
		}else {
			Log.i("test", "isValidate=false");
		}
	}
	public void testRenRen() throws Throwable{
		Bundle bundle = InfoUtil.getUserCenterInfo(getContext());
		Log.i("test", "bundle="+bundle);
	}
	
	public void testLogin() throws Throwable {
		final String IP=LoginActivity.IP;
		final String APPCODE=InfoUtil.APPCODE;
		final String KEY=InfoUtil.KEY;
		final String path=IP+"/Mobile/login";
		String account="gaox@test.com";
		String password="123456";
		final String psd=LoginActivity.phpMD5Encode(password);
		String reqstr=Base64Encoder.encode(("user=" +account+"&password="+psd).getBytes("UTF-8"));
		String verify=LoginActivity.phpMD5Encode("user="+account+"&password="+psd+KEY);
		
		DefaultHttpClient client= new DefaultHttpClient();
		HttpPost post=new HttpPost(path);
		ArrayList<NameValuePair> valuePairs=new ArrayList<NameValuePair>();
		BasicNameValuePair valuePair=new BasicNameValuePair("appcode",APPCODE);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("reqstr",reqstr);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("verify",verify);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("datatype","j");
		valuePairs.add(valuePair);
		
		UrlEncodedFormEntity entity=new UrlEncodedFormEntity(valuePairs, "UTF-8");
		post.setEntity(entity);
		HttpResponse response = client.execute(post);
		HttpEntity httpEntity = response.getEntity();
		if (httpEntity!=null) {
			parseInputStream(httpEntity.getContent());
		}
	}
	private void parseInputStream(InputStream inputStream) throws Exception {
		byte[] data = LoginActivity.getDataFromInputStream(inputStream);
		if (data!=null&&data.length>0) {
			String json=new String(data, "UTF-8");
			Log.i("test", json);
			JSONObject obj=new JSONObject(json);
			int code = obj.optInt("code");
			if (code==1) {
				int userId = obj.optInt("userid");
				String token=obj.optString("token");
				Log.i("test", "userId="+userId);
				Log.i("test", "token="+token);
			}else {
				throw new RuntimeException(obj.optString("description"));
			}
		}else {
			throw new RuntimeException("登录失败");
		}
	}
}
